<template>
    <div>
        <el-row :gutter="20" style="padding: 20px;margin: 0px">
            <el-col :span="24">
                <el-form ref="form" label-width="70px">
                    <el-col :span="12" class="mt">
                        <el-form-item label="员工姓名">
                            <el-input placeholder="请输入员工姓名" v-model="name"></el-input>
                        </el-form-item>
                        <el-form-item label="员工性别">
                            <el-radio-group v-model="sex">
                                <el-radio name="sex" label="1">男士</el-radio>
                                <el-radio name="sex" label="0">女士</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="员工工号">
                            <el-input placeholder="请输入员工工号" v-model="worknum"></el-input>
                        </el-form-item>
                        <el-form-item label="员工帐号">
                            <el-input placeholder="请输入员工帐号（手机号）" v-model="username"></el-input>
                        </el-form-item>
                        <el-form-item label="员工类型">
                            <el-radio-group v-model="ismanage">
                                <el-radio name="manage" label="1">管理层</el-radio>
                                <el-radio name="manage" label="0">非管理层</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="mt">
                        <el-form-item label="所属部门">
                            <el-input :placeholder="bumen" icon="search" v-model="bumen" disabled>
                                <el-button slot="append" @click="selectBumen" icon="el-icon-h-search"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="所属职位">
                            <el-select v-model="positionid" :placeholder="positionname" @change="selectPos" class="width">
                                <el-option v-for="(item,index) in plist" :label="item.name" :value="item.id" :key="index">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="所属职称">
                            <el-select v-model="zhichengid" :placeholder="zhichengname" class="width">
                                <el-option v-for="(item,index) in zlist" :label="item.name" :value="item.id" :key="index">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="员工描述">
                            <el-input type="textarea" placeholder="请输入员工描述" v-model="remarks">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button size="mini" type="primary" @click="submit">保存</el-button>
                            <el-button size="mini" @click="closeSub">取消</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'addUser',
  data () {
    return {
      id: 0,
      name: '',
      sex: '0',
      username: '',
      bumen: '请选择部门！',
      bumenid: 0,
      positionid: '',
      positionname: '请选择职位！',
      zhichengid: '',
      zhichengname: '请选择职称！',
      remarks: '',
      ismanage: '0',
      worknum: '',
      plist: [],
      zlist: []
    }
  },
  methods: {
    async selectBumen () {
      let res = await C.openBrowserWindow('/selectDepartment', { bmid: this.bumenid }, { width: 600, height: 700, useSpa: false, spaModel: 'shop', transparent: true, frame: false })
      if (res) {
        this.bumen = res.name
        this.bumenid = res.id
      }
    },
    async selectPos () {
      let data = await C.ajax('/shop/public/getdplist', { id: this.positionid }, 'post')
      this.zhichengid = ''
      if (data) this.zlist = data
    },
    async getList () {
      let data = await C.ajax('/shop/public/getdplist')
      if (data) this.plist = data.plist
    },
    async submit () {
      if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(this.name))) {
        C.error('姓名格式不正确')
        return false
      } else if (!(/^1[345678]\d{9}$/.test(this.username))) {
        C.error('帐号(手机号)不正确')
        return false
      } else if (this.worknum == '') {
        C.error('请填写员工工号')
        return false
      } else if (this.bumenid == 0) {
        C.error('请选择部门')
        return false
      } else if (this.positionid == 0) {
        C.error('请选择职位')
        return false
      } else if (this.zhichengid == 0) {
        C.error('请选择职称')
        return false
      }
      var aaa = false
      this.zlist.forEach((row) => {
        if (this.zhichengid === row.id) {
          aaa = true
        }
      })
      var data = {
        username: this.username,
        name: this.name,
        worknum: this.worknum,
        sex: this.sex,
        positionid: this.positionid,
        zhichengid: this.zhichengid,
        departmentid: this.bumenid,
        ismanage: this.ismanage,
        remarks: this.remarks
      }
      let url = ''
      if (this.id * 1) {
        data.id = this.id
        url = '/shop/user/edituser'
      } else {
        url = '/shop/user/adduser'
      }
      if (aaa) {
        let res = await C.ajax(url, data, 'post')
        if (res) C.closeBrowserWindow(res)
      } else {
        C.error('请重新选择职称')
        return false
      }
    },
    async closeSub () {
      C.closeBrowserWindow()
    }
  },
  created: async function () {
    let row = C.getDatas()
    if (row.id) {
      this.zlist = await C.ajax('/shop/public/getdplist', { id: row.pid }, 'post')
      this.id = row.id
      this.name = row.name
      this.sex = row.sex.toString()
      this.username = row.username
      this.bumen = row.departmentname
      this.bumenid = row.departmentid
      this.positionid = row.pid
      this.positionname = row.positionname
      this.zhichengid = row.sid
      this.zhichengname = row.zhichengname
      this.remarks = row.remarks
      this.worknum = row.worknum
      this.ismanage = row.ismanage.toString()
    }
    this.getList()
  }
}
</script>
<style>
</style>